package com.szgz.platform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szgz.platform.model.TeacherEnvironment;
import com.szgz.platform.model.TeacherEnvironmentUsage;
import com.szgz.platform.result.ApiResponse;
import com.szgz.platform.result.PageResult;
import com.szgz.platform.service.TeacherEnvironmentService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher/envs")
@CrossOrigin(origins = "*")
public class TeacherEnvironmentController {

	private final TeacherEnvironmentService envService;

	public TeacherEnvironmentController(TeacherEnvironmentService envService) {
		this.envService = envService;
	}

	@PostMapping
	public ApiResponse<TeacherEnvironment> create(@RequestBody TeacherEnvironment env) {
		return ApiResponse.success(envService.create(env));
	}

	@GetMapping
	public ApiResponse<PageResult<TeacherEnvironment>> page(@RequestParam(defaultValue = "1") long pageNum,
	                                                       @RequestParam(defaultValue = "10") long pageSize,
	                                                       @RequestParam(required = false) String teacherId,
	                                                       @RequestParam(required = false) String groupId,
	                                                       @RequestParam(required = false) String type,
	                                                       @RequestParam(required = false) String status) {
		Page<TeacherEnvironment> page = envService.page(pageNum, pageSize, teacherId, groupId, type, status);
		return ApiResponse.success(PageResult.from(page));
	}

	@GetMapping("/{environmentId}")
	public ApiResponse<TeacherEnvironment> getById(@PathVariable String environmentId) {
		TeacherEnvironment env = envService.getById(environmentId);
		return env != null ? ApiResponse.success(env) : ApiResponse.fail(404, "Not Found");
	}

	@DeleteMapping("/{environmentId}")
	public ApiResponse<Void> delete(@PathVariable String environmentId) {
		return envService.deleteEnvironment(environmentId) ? ApiResponse.success() : ApiResponse.fail(404, "Not Found");
	}

	@PostMapping("/{environmentId}/usages")
	public ApiResponse<List<TeacherEnvironmentUsage>> bindUsers(@PathVariable String environmentId,
	                                                          @RequestBody List<TeacherEnvironmentUsage> usages) {
		return ApiResponse.success(envService.bindStudents(environmentId, usages));
	}

	@GetMapping("/{environmentId}/usages")
	public ApiResponse<PageResult<TeacherEnvironmentUsage>> listUsages(@PathVariable String environmentId,
	                                                                  @RequestParam(defaultValue = "1") long pageNum,
	                                                                  @RequestParam(defaultValue = "10") long pageSize) {
		Page<TeacherEnvironmentUsage> page = envService.pageUsages(environmentId, pageNum, pageSize);
		return ApiResponse.success(PageResult.from(page));
	}

	@PutMapping("/usages/{usageId}")
	public ApiResponse<TeacherEnvironmentUsage> updateUsage(@PathVariable String usageId,
	                                                      @RequestBody TeacherEnvironmentUsage usage) {
		usage.setId(usageId);
		return ApiResponse.success(envService.updateUsage(usage));
	}

	@DeleteMapping("/usages/{usageId}")
	public ApiResponse<Void> deleteUsage(@PathVariable String usageId) {
		return envService.deleteUsage(usageId) ? ApiResponse.success() : ApiResponse.fail(404, "Not Found");
	}

	@PutMapping("/{environmentId}/status")
	public ApiResponse<Void> updateStatus(@PathVariable String environmentId, @RequestParam String status) {
		return envService.updateStatus(environmentId, status) ? ApiResponse.success() : ApiResponse.fail(404, "Not Found");
	}
}

